import VueLazyLoad from "vue-lazyload";
import Scroll from "../components/common/Scroll";
import "./plugin.scss";
import $ from "jquery";

export default {
  install(Vue) {
    //图片懒加载组件
    Vue.use(VueLazyLoad, {
      preLoad: 1.3,
      error:require('../../public/img/music.png'),
      loading: require('../../public/img/music.png'),
      attempt: 1,
    });
    //滚动组件
    Vue.component("scroll", Scroll);

    //loading组件
    const Loading = Vue.extend({
      render() {
        return (
          <div id="loading">
            <span></span>
            <span></span>
          </div>
        );
      },
    });
    //全局mixin
    Vue.mixin({
      watch: {
        loading(newVal) {
          if(this.needLoading) {
            newVal?this.$showLoading():this.$hideLoading()
          }
        },
      },
      methods: {
        $showLoading() {
          $("#wrap").append(`<div id="loading"></div>`);
          this.loadComp = new Loading();
          this.loadComp.$mount("#loading");
        },
        $hideLoading() {
          this.loadComp.$destroy();
          $("#wrap").empty();
        },
        $toast(val){
          $("#wrap").empty();
          const Toast=Vue.extend({
            render(){return (<div id='toast'>{val}</div>)}
          })
          $("#wrap").append(`<div id="toast"></div>`)
          this.toastComp=new Toast()
          this.toastComp.$mount('#toast')
          clearInterval(this.time)
          this.time=setTimeout(()=>{
            this.toastComp.$destroy()
            $("#wrap").empty();
          },1500)
        }
      },
    });
    //全局过滤器
    Vue.filter("totalFor", (count) => {
      if (count < 10000) {
        return count;
      } else {
        return ~~(count / 10000) + "万";
      }
    });
  },
};
